#include "spicy.h"
#include "vcd-get-login-ticket.h"
#include "vcd-refresh-vm-info.h"
#include "vcd-vm-connect.h"
#include "vcd-get-vm-info.h"
#include "vcd-conf/vcd-conf.h"
#include "vcd-json-parser.h"
#include "vcd-common.h"
#include "vcd-linux-ui/vcd-message-dialog.h"
#include "vcd-linux-ui/vcd-logined-window.h"
#include "vcd-util.h"
#include "vcd-download.h"
#include "vcd-linux-ui/vcd-start-window.h"
#include "vcd-new-login-mode.h"
#include "vcd-linux-ui/vcd-setting-window/setting-network/vcd-wire.h"
#include "vcd-linux-ui/vcd-setting-window/setting-main/vcd-sys-info.h"

static gboolean check_connecting_num_func(gpointer data)
{
    g_return_val_if_fail(data != NULL, FALSE);
    if (data == NULL) {
        delete_g_connect_label_and_spinner();
        return FALSE;
    }

    M_DEBUG("check_connecting_num_func");
    int pre_link = 0;
    int ret = 0;
    check_num_ctx *ctx = NULL;
    network_response *res = (network_response*)data;
    ret = parse_connecting_num(res->memory, &pre_link);
    if (ret != 0)
        goto fail;

    if (pre_link != 0) {
        char szbuf[255];
        switch (pre_link) {
            case  1: snprintf(szbuf, sizeof(szbuf), " 连接限制!(vmid=%d) ", connect_data.vmid); break;
            case  2: snprintf(szbuf, sizeof(szbuf), " 此桌面正在被连接请稍后再试!(vmid=%d) ", connect_data.vmid); break;
            case -1: snprintf(szbuf, sizeof(szbuf), " 连接请求服务器失败!(vmid=%d) ", connect_data.vmid); break;
            default: M_DEBUG("/api/pool/vmConnect接口返回default(vmid=%d) ", connect_data.vmid); break;
        }
        message_box(szbuf);
        goto fail;
    }

    if (res->data_priv == NULL)
        goto fail;

    ctx = (check_num_ctx*)res->data_priv;
    spicy_connect(ctx->ip, ctx->tls_port, ctx->password, NULL);

fail:
    delete_g_connect_label_and_spinner();
    network_response_free(&res);
    return FALSE;
}

static gboolean virt_client_disconnect_func(gpointer data)
{
    g_return_val_if_fail(data != NULL, FALSE);
    disconnect_res d;
    network_response *res = (network_response*)data;
    if (res->memory == NULL) {
        LOG_ERROR("res->memory == NULL");
        goto fail;
    }

    parse_client_disconnect(res->memory, &d);
    //do nothing
fail:
    network_response_free(&res);
    return FALSE;
}

void virt_client_disconnect(void)
{
    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;

    if (PUBLIC_LOGIN == tmp_vcd_conf->login_way || EMODE_OLD == tmp_vcd_conf->login_mode)
        return;

    struct curl_slist *list = NULL;
    list = curl_slist_append(list, g_new_resources_ticket);

    char url2[200] = "https://";
    strcat(url2, get_ip_port());

    strcat(url2, "/api/pool/vmReset");

    char post_fields[40] = "vmid=";
    char stringvmid[10];
    sprintf(stringvmid, "%d", connect_data.vmid);
    strcat(post_fields, stringvmid);
    strcat(post_fields, "&node=");
    strcat(post_fields, connect_data.node);

    network_request_arg *req = (network_request_arg*)malloc(sizeof(network_request_arg));
    g_return_if_fail(req != NULL);

    strncpy(req->url, url2, sizeof(req->url));
    req->http_method = CURLOPT_POST;
    strncpy(req->post_fields, post_fields, sizeof(req->post_fields));
    req->timeout = 3;
    req->list = list;
    req->data_priv = NULL;
    req->callback = virt_client_disconnect_func;

    net_req_list_add(req);
}

void check_num_ctx_free(check_num_ctx** pp)
{
    if (pp == NULL || *pp == NULL)
        return;

    check_num_ctx *p = *pp;
    SAFE_FREE(p->ip);
    SAFE_FREE(p->tls_port);
    SAFE_FREE(p->password);
    SAFE_FREE(p);
    *pp = NULL;
}

int check_new_mode_connect(check_num_ctx *ctx)
{
    M_DEBUG("check_new_mode_connect");
    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;

    struct curl_slist *list = NULL;
    list = curl_slist_append(list, g_new_resources_ticket);

    char url2[200] = {0};
    sprintf(url2, "https://%s/api/pool/vmConnect", get_ip_port());

    char post_fields[200] = "vmid=";
    char stringvmid[10];
    sprintf(stringvmid, "%d", connect_data.vmid);
    strcat(post_fields, stringvmid);

    strcat(post_fields, "&ip=");
    char *tmp_ip = get_nowip();
    if (is_num_dot(tmp_ip))
        strcat(post_fields, tmp_ip);
    else
        strcat(post_fields, "none");
    if (tmp_ip)
        free(tmp_ip);

    strcat(post_fields, "&mac=");
    char *tmp_mac = get_mac_();
    strcat(post_fields, tmp_mac);
    if (tmp_mac)
        free(tmp_mac);

    strcat(post_fields, "&username=");
    strcat(post_fields, tmp_vcd_conf->username);

    strcat(post_fields, "&type=");
    strcat(post_fields, "spice");

    network_request_arg *req = (network_request_arg*)malloc(sizeof(network_request_arg));
    g_return_val_if_fail(req != NULL, -1);
    strncpy(req->url, url2, sizeof(req->url));
    req->http_method = CURLOPT_POST;
    strncpy(req->post_fields, post_fields, sizeof(req->post_fields));
    req->timeout = 3;
    req->list = list;
    req->data_priv = (gpointer)ctx;
    req->callback = check_connecting_num_func;

    net_req_list_add(req);
    return 0;
}
